import Vue from "vue";
import VueRouter from "vue-router";

Vue.use(VueRouter);

const routes = [
  {
    path: "/",
    redirect: "Login",
  },
  {
    path: "/manager",
    name: "Manager",
    component: () => import("@views/Manager/ManageReadingRoom"),
    meta: {
      requiresAuth: true,
    },
    children: [
      {
        path: "rooms",
        name: "m_Rooms",
        component: () => import("@views/Manager/Room/Room"),
        meta: {
          requiresAuth: true,
        },
      },
      {
        path: "add_room",
        name: "m_addRoom",
        component: () => import("@views/Manager/Room/AddRoom"),
        meta: {
          requiresAuth: true,
        },
      },
      {
        path: "users",
        name: "m_checkUsers",
        component: () => import("@views/Manager/Users/Users"),
        meta: {
          requiresAuth: true,
        },
      },
      {
        path: "add_user",
        name: "m_addUser",
        component: () => import("@views/Manager/Users/AddUser"),
        meta: {
          requiresAuth: true,
        },
      },

      {
        path: "check_notice",
        name: "m_checkNotice",
        component: () => import("@views/Manager/Notice/Notice"),
        meta: {
          requiresAuth: true,
        },
      },
      {
        path: "post_notice",
        name: "m_postNotice",
        component: () => import("@views/Manager/Notice/PostNotice"),
        meta: {
          requiresAuth: true,
        },
      },
      {
        path: "feedback",
        name: "m_feedback",
        component: () => import("@views/Manager/Library/Feedback"),
        meta: {
          requiresAuth: true,
        },
      },
    ],
  },
  {
    path: "/student",
    name: "Student",
    component: () => import("@views/Student/StudentReadingRoom"),
    meta: {
      requiresAuth: true,
    },
    children: [
      {
        path: "s_appointment",
        name: "s_Appointment",
        component: () =>
          import("@views/Student/Appointment/StudentAppointment"),
        meta: {
          requiresAuth: true,
        },
      },
      {
        path: "rooms",
        name: "s_Rooms",
        component: () => import("@views/Student/Room/Room"),
        meta: {
          requiresAuth: true,
        },
      },
      {
        path: "/feedback",
        name: "s_Feedback",
        component: () => import("@views/Student/Feedback/Feedback"),
        meta: {
          requiresAuth: true,
        },
      },
      {
        path: "/my_feedback",
        name: "s_MyFeedback",
        component: () => import("@views/Student/Feedback/MyFeedback"),
        meta: {
          requiresAuth: true,
        },
      },
    ],
  },
  {
    path: "/login",
    name: "Login",
    component: () => import("@views/User/Login"),
  },
  {
    path: "*",
    name: "NotFound",
    component: () => import("@components/content/NotFound"),
  },
];

const router = new VueRouter({
  mode: "history",
  base: process.env.BASE_URL,
  routes,
});

router.beforeEach((to, from, next) => {
  //判断是否需要登录
  let token = localStorage.library_sys_token;
  if (token && to.path == "/login") {
    next("/Home");
  } else if (to.meta.requiresAuth && !token) {
    next("/login");
  } else {
    next();
  }
});

export default router;
